package com.homework;

import java.sql.*;

/**
 * @author: XiangHu
 * @date: 2025年02月26日 12:01
 * @describe:
 */
public class Login {
    public static boolean login(String username, String password) {
        String sql = "SELECT * FROM h_user WHERE username = ? AND password = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, username);
            ps.setString(2, password);

            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("用户名或密码错误，请重新输入");
        return false;
    }

    public static boolean isAdmin(String username) {
        String sql = "SELECT is_admin FROM h_user WHERE username = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, username);

            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return rs.getBoolean("is_admin");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}